package com.ctyun.xstore.s3.api.object;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.ctyun.xstore.s3.client.AmazonS3ClientBuilder;

import java.util.List;


public class ListObjects {
    public static void listObjects(String bucketName,String filePath) {
        AmazonS3 s3 = AmazonS3ClientBuilder.buildAmazonS3Client();
        ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
        if (filePath != null && !filePath.equals("")) {
            req.setPrefix(filePath);
        }
        //设置展示的最大记录数
        req.setMaxKeys(2000);
        ListObjectsV2Result result = s3.listObjectsV2(req);
        List<S3ObjectSummary> objects = result.getObjectSummaries();
        System.out.println("总记录数："+objects.size());
        for (S3ObjectSummary os : objects) {
            System.out.println("* " + os.getKey());
        }
    }
}
